package logger;

import java.io.Serializable;

import javax.enterprise.context.Conversation;
import javax.enterprise.context.ConversationScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;

import lombok.Getter;
import lombok.Setter;

@Named
@ConversationScoped
public class LogaEmArquivo implements Serializable {

	private static final long serialVersionUID = 1L;

	@Getter
	@Setter
	private String mensagem;
	
	@Inject
	private Conversation conversation;
	
	@Inject
	LoggerComCDI logger = new LoggerComCDI();

	public void gravar() {
		iniciaConversacao();
		logger.gravar(mensagem);
		FacesContext.getCurrentInstance().addMessage(null,
				new FacesMessage("Mensagem gravada com sucesso!"));
		terminaConversacao();
	}

	public void iniciaConversacao() {
		if (conversation.isTransient()) {
			conversation.begin();
		}
	}

	public void terminaConversacao() {
		if (!conversation.isTransient()) {
			conversation.end();
		}
	}

}
